package com.shushan.discount;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.discount.bean.CouponBean;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: CouponsAdapter
 * @Desciption: //优惠券显示适配器
 * @author: zhangshihao
 * @date: 2018-08-30
 */
public class CouponsAdapter extends RecyclerView.Adapter<CouponsAdapter.ViewHolder> {

    private static final String TAG = CouponsAdapter.class.getSimpleName();

    private List<CouponBean> couponBeans;
    private int type;
    private int useType;
    private OnBookPageClickListener onBookPageClickListener;
    /**
     * @param couponBeans
     * @param type        0：自用券；1：亲情券
     * @param useType     0:未使用；1：已使用；2：已过期
     */
    public CouponsAdapter(List<CouponBean> couponBeans, int type, int useType) {
        this.couponBeans = couponBeans;
        this.type = type;
        this.useType = useType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discount, null, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(TAG,"useType == "+useType+" ; type == "+type);
        final CouponBean couponBean = couponBeans.get(position);
        if (type == 1) {//亲情券
            holder.rlCouponValueBg.setBackgroundResource(R.drawable.ic_purple_discount);
            holder.ivCouponIcon.setImageResource(R.drawable.ic_purple_discount_code);
            holder.ivBookPage.setImageResource(R.drawable.ic_purple_book_page);
            if (couponBean.is_use == 1) {//已使用
                StringBuffer sb = new StringBuffer();
                sb.append("好友\n")
                        .append(getDisplayName(couponBean.name))
                        .append("\n")
                        .append("已使用");
                holder.tvBookPageText.setText(sb.toString());
            } else {
                holder.tvBookPageText.setText("送给好友");
            }
        }
        holder.tvCouponValue.setText(String.valueOf(couponBean.price));
        holder.tvCouponName.setText(couponBean.coupon_name);
        holder.tvCouponCode.setText(couponBean.code);
        holder.tvUsefulLife.setText("有效期：" + couponBean.exp_date);
        if(useType == 1){//已使用
            holder.ivBookPage.setImageResource(R.drawable.ic_gray_book_page);
        }
        if(useType == 2){//已过期
            holder.rlCouponValueBg.setBackgroundResource(R.drawable.ic_gray_discount);
            holder.ivCouponIcon.setImageResource(R.drawable.ic_gray_discount_code);
            holder.ivBookPage.setImageResource(R.drawable.ic_gray_book_page);
        }
        holder.rlBookPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onBookPageClickListener != null){
                    onBookPageClickListener.onBookPageClick(useType,type,couponBean);
                }
            }
        });
    }

    private String getDisplayName(String name) {
        if (TextUtils.isEmpty(name)) {
            return "";
        }
        if (name.length() <= 3) {
            return name;
        } else {
            return name.substring(0, 2) + "...";
        }
    }

    @Override
    public int getItemCount() {
        return couponBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rlCouponValueBg;
        private TextView tvCouponValue;
        private TextView tvCouponName;
        private ImageView ivCouponIcon;
        private TextView tvCouponCode;
        private TextView tvUsefulLife;
        private ImageView ivBookPage;
        private TextView tvBookPageText;
        private RelativeLayout rlBookPage;

        public ViewHolder(View itemView) {
            super(itemView);
            rlCouponValueBg = itemView.findViewById(R.id.rl_bg_coupon_value);
            tvCouponValue = itemView.findViewById(R.id.tv_discount_value);
            tvCouponName = itemView.findViewById(R.id.tv_discount_name);
            ivCouponIcon = itemView.findViewById(R.id.iv_coupon_icon);
            tvCouponCode = itemView.findViewById(R.id.tv_coupon_code);
            tvUsefulLife = itemView.findViewById(R.id.tv_useful_life);
            ivBookPage = itemView.findViewById(R.id.iv_book_page);
            tvBookPageText = itemView.findViewById(R.id.tv_book_page_text);
            rlBookPage = itemView.findViewById(R.id.rl_book_page);
        }
    }

    public interface OnBookPageClickListener{
        void onBookPageClick(int useType,int couponType,CouponBean couponBean);
    }

    public void setOnBookPageClickListener(OnBookPageClickListener onBookPageClickListener) {
        this.onBookPageClickListener = onBookPageClickListener;
    }
}
